<?php

namespace app\admin\service\setting;

use app\common\model\setting\ConfigUpload;
use tmcore\base\BaseService;
use tmcore\services\ConfigService;


/**
 * 存储设置服务类
 */
class StorageService extends BaseService
{

    /**
     * 存储引擎列表
     * @return array
     */
    public static function lists()
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status'],
        ];
        $where = self::getSearchData($searchParam);

        return ConfigUpload::where($where)
            ->field('id,name,identify,is_default')
            ->order([ 'id' => 'asc'])
            ->select()
            ->toArray();
    }


    /**
     * 存储设置详情
     * @param $params
     * @return mixed
     */
    public static function detail($params)
    {
        return ConfigUpload::where(['identify' => $params['identify']])->findOrEmpty()->toArray();
    }


    /**
     * 设置存储参数
     * @param $params
     * @return bool
     */
    public static function edit($params)
    {
        self::beginTrans();
        try {
            switch ($params['identify']) {
                case 'local':
                    $params['params'] = [];
                    break;
                case 'aliyun':
                    $params['params'] = [
                        'bucket' => $params['bucket'] ?? '',
                        'accessKeyId' => $params['accessKeyId'] ?? '',
                        'accessSecret' => $params['accessSecret'] ?? '',
                        'endpoint' => $params['endpoint'] ?? '',
                        'domain' => $params['domain'] ?? ''
                    ];

                    break;
                case 'qiniu':
                    $params['params'] = [
                        'bucket' => $params['bucket'] ?? '',
                        'accessKey' => $params['accessKey'] ?? '',
                        'secretKey' => $params['secretKey'] ?? '',
                        'domain' => $params['domain'] ?? ''
                    ];
                    break;
                case 'qcloud':
                    $params['params'] = [
                        'bucket' => $params['bucket'] ?? '',
                        'accessKey' => $params['accessKey'] ?? '',
                        'secretKey' => $params['secretKey'] ?? '',
                        'region' => $params['region'] ?? '',
                        'domain' => $params['domain'] ?? ''
                    ];
                    break;
                default:
                    $params['params'] = [];
                    break;
            }

            if(!$params['status']) {
                $identify = ConfigUpload::where(['is_default' => 1])->value('identify');
                if($identify == $params['identify']) throw new \Exception('默认存储引擎不可关闭');
            }

            ConfigUpload::update([
                'status' => $params['status'],
                'params' => $params['params'],
            ],['identify' => $params['identify']]);

            self::commitTrans();
            // 刷新缓存
            (new ConfigService)->setConfig('create', 'upload', $params['identify']);
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }


    /**
     * 切换状态
     * @param $params
     * @return bool
     */
    public static function change($params)
    {
        self::beginTrans();
        try {
            $is_default = ConfigUpload::where(['identify' => $params['identify']])->value('is_default');

            if(!$is_default) {
                ConfigUpload::update(['is_default' => 1],['identify' => $params['identify']]);
                ConfigUpload::where([['identify', '<>', $params['identify']]])->update(['is_default' => 0]);
            }
            self::commitTrans();
            if(!$is_default) {
                // 刷新缓存
                (new ConfigService)->setConfig('create', 'upload', $params['identify']);
            }
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }
}